package com.qf.business.system.controller;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.qf.business.system.service.RoleService;
import com.qf.data.base.r.R;
import com.qf.data.base.r.Rs;
import com.qf.data.base.utilis.QfBeanUtils;
import com.qf.data.system.dto.RoleDto;
import com.qf.data.system.entity.Role;
import com.qf.data.system.vo.input.RoleInput;
import com.qf.data.system.vo.output.RoleListOutput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色列表 - 部门名称
     *
     * @return
     */
    @RequestMapping("/rolelist")
    public R list() {
        List<RoleDto> roles = roleService.queryList();
        List<RoleListOutput> roleListOutputs = QfBeanUtils.listCopy(roles, RoleListOutput.class);
        return Rs.create(roleListOutputs);
    }

    /**
     * 新增角色
     *
     * @param roleInput
     * @return
     */
    @RequestMapping("/roleinsert")
    public R insert(@Validated RoleInput roleInput) {
        Role role = QfBeanUtils.beanCopy(roleInput, Role.class);
        roleService.save(role);
        return Rs.create("新增角色成功");
    }

    @RequestMapping("/updatePower")
    public R updatePower(Long rid, String pids) {
        log.info("根据角色权限修改：{}-{}", rid, pids);
        List<Long> parseArray = JSON.parseArray(pids, Long.class);
        roleService.updatePowerByRid(rid, parseArray);
        return Rs.create("succ");
    }

    /**
     * 修改角色
     *
     * @param roleInput 只在mybatispuls才有效果
     * @return
     */
    @RequestMapping("/roleupdate")
    public R update(RoleInput roleInput, Long rid) {
        Role role = new Role();
        role.setId(rid);
        roleService.updateById(role);
        return null;
    }
}
